package view;

import inventory.InventoryItem;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Model;
import model.UserPage;
import user.User;

/**
 * UserPageDiscoverXML implements the Discovery XML display of a user's
 * signed-in dashboard, populating the display with Lithub user book information
 * which can be used by a user to find books in the Lithub database.
 *
 * Primarily used for the Android side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class UserPageDiscoverXML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * UserPageDiscoverXML(...) is a reciever of user dashboard page view
     * requests for the web baased Discovery Div in XML and also the responder
     * to such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    UserPageDiscoverXML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of the
     * SearchPage/Discovery page model implementation to try to provide a user
     * with Lithub database book information in XML.
     *
     * @param a model which refers to the model that implements the
     * UserPageDiscoverDiv.
     *
     * @catch generic exception.
     *
     */
    @Override
    public void invoke(Model m) {
        PrintWriter out = null;
        try {
            UserPage u = (UserPage) m;

            out = response.getWriter();
            response.setContentType("text/xml");
            out.print("<body copyright='All data copyright LitHub 2012'>");

            ArrayList<User> listOfOwners;
            int divID = 0;

            for (InventoryItem key : u.library.keySet()) {
                listOfOwners = u.library.get(key);

                out.print("<book>");

                out.print("<isbn>" + key.itemISBN + "</isbn>");
                out.print("<title>" + key.itemName + "</title>");
                out.print("<author>" + key.itemAuthor + "</author>");
                out.print("<status>" + key.status + "</status>");
                for (int i = 0; i < listOfOwners.size(); ++i) {
                    out.print("<owner>" + listOfOwners.get(i).username + "</owner>");
                }

                out.print("</book>");
            }
            out.print("</body>");
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
